//
//  BuzzAttachment.m
//  Buzz API Demo
//
//  Created by Peter Hopkins on 5/10/10.
//  Copyright 2010 Google Inc. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "BuzzAttachment.h"

#import "ActivityStreamsElements.h"
#import "BuzzConstants.h"

#import "GDataMediaGroup.h"

#define kWidthAttr @"width"
#define kHeightAttr @"height"

@interface MediaWidth : GDataAttribute <GDataExtension>
@end

@interface MediaHeight : GDataAttribute <GDataExtension>
@end

@implementation BuzzAttachment

+ (NSString *)extensionElementURI { return kBuzzNamespace; }
+ (NSString *)extensionElementPrefix { return kBuzzNamespacePrefix; }
+ (NSString *)extensionElementLocalName { return @"attachment"; }

- (void)addExtensionDeclarations {
  [super addExtensionDeclarations];
  
  [self addExtensionDeclarationForParentClass:[self class]
                                 childClasses:[ActivityObjectType class], [GDataLink class], nil];

  [self addAttributeExtensionDeclarationForParentClass:[GDataLink class]
                                            childClass:[MediaWidth class]];
  [self addAttributeExtensionDeclarationForParentClass:[GDataLink class]
                                            childClass:[MediaHeight class]];
}


- (NSString *)objectType {
  return [[self objectForExtensionClass:[ActivityObjectType class]] stringValue];
}


- (GDataLink *)previewLink {
  NSArray *links = [self objectsForExtensionClass:[GDataLink class]];
  for (GDataLink *link in links) {
    if ([[link rel] isEqualToString:@"preview"]) {
      return link;
    }
  }

  return nil;
}


+ (NSNumber *)widthForPreviewLink:(GDataLink *)link {
  NSString *str = [link attributeValueForExtensionClass:[MediaWidth class]];
  if ([str length] > 0) {
    NSNumber *number = [NSNumber numberWithInt:[str intValue]];
    return number;
  }
  return nil;
}


+ (NSNumber *)heightForPreviewLink:(GDataLink *)link {
  NSString *str = [link attributeValueForExtensionClass:[MediaHeight class]];
  if ([str length] > 0) {
    NSNumber *number = [NSNumber numberWithInt:[str intValue]];
    return number;
  }
  return nil;
}

@end


@implementation MediaWidth
+ (NSString *)extensionElementURI { return kGDataNamespaceMedia; }
+ (NSString *)extensionElementPrefix { return kGDataNamespaceMediaPrefix; }
+ (NSString *)extensionElementLocalName { return kWidthAttr; }
@end


@implementation MediaHeight
+ (NSString *)extensionElementURI { return kGDataNamespaceMedia; }
+ (NSString *)extensionElementPrefix { return kGDataNamespaceMediaPrefix; }
+ (NSString *)extensionElementLocalName { return kHeightAttr; }
@end
